﻿using CKS.Core.Framework;
using CKS.PLC.Core;
using CKS.PLC.Core.Cache;
using FreeSql;
using Infrastructure;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.Models.ProductionControl;
using ZR.ServiceCore.Services;

namespace CKS.PLC.Action.Subscription {

    /// <summary>
    /// 涂胶机使能定订阅点
    /// </summary>
    [SubscribeAction("GluingEnableForwardSubscribe")]
    public class GluingEnableForwardSubscribe : SubscribeBase {

        public GluingEnableForwardSubscribe(DecodeValue decodeValue) : base(decodeValue) {

            
           
        }


        public override void ExecuteAction(Dictionary<string, object> execParams) {
            try {

                using (var ObjectCo = base.FreeSql.Ado.MasterPool.Get()) {
                    //db
                    var conn = ObjectCo.Value;
                    var position = DecodeValue.Position;
                    var result = base.ReadDataBool(position).GetAwaiter().GetResult();

                    var sysConfigService = App.GetService<ISysConfigService>();
                    var configValue = sysConfigService.GetSysConfigByKey("gluingEnableForward");
                    JArray jArray = JArray.Parse(configValue.ConfigValue);

                    foreach (var item in jArray) {
                        //是否开启
                        var isEnabled = item["IsEnabled"].ToBool();
                        if (isEnabled) {
                            //当前待验证工位信息 121|1030
                            var curStationID = item["CurStation"].ToString().Split("|")[0].ToInt();
                            var curStationDBblock = item["CurStation"].ToString().Split("|")[1].ToInt();
                            //需要转发的工位
                            var forwardStationID = item["ForwardStation"].ToString().Split("|")[0].ToInt();
                            var forwardStationDBblock = item["ForwardStation"].ToString().Split("|")[1].ToInt();

                            if (position.PlcId == curStationID && position.BlockDb == curStationDBblock) {
                                var forwardPlcSession = PlcCache.PlcClients.ToList().FirstOrDefault(o => o.PlcInfo.Id == forwardStationID);
                                var forwardPlcPostion = conn.Select<CksPlcDataPosition>().Where(o => o.PlcId == forwardStationID && o.BlockDb == forwardStationDBblock && o.ActionKey == "GluingEnableForwardSubscribe").ToList().FirstOrDefault();
                                if (forwardPlcSession != null && forwardPlcSession.IsConnected && forwardPlcPostion != null) {
                                    forwardPlcSession.SetValue(forwardPlcPostion, result.ToString());
                                }
                            }

                        }
                    }

                }

            } catch (Exception ex) {
                EventBus.AddLogErrorToQueues($"{ex.Message}");
            }
        }

    }
}
